// keyboard.c
//
// Keyboard Reader - emulate a hardware interrupt
// read the keyboard and signal the parent process when a key is received
//
//+++++++++++++++++++++++
// modifed to use the POSIX-style of obtaining shared memory
// by P. Dasiewicz, June 5, 2007
//+++++++++++++++++++++++++

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

#include "global.h"
#include "kernel.h"
#include "kernel_classes.cpp"
#include "kernel_rtx.cpp"

void start(){
     MsgEnv* begin=request_msg_env();
     rtx->send_message(ProcessA,begin);
}
void list_statuses(){
     for(int i=0;i<Num_Proc;i++)
         printf("%i %i\n",PCB_list[i]->pid,PCB_list[i]->status);
}
void set_wall_clock(int h, int m, int s){
     set_clock(h,m,s);
}
void display_clock(){
     clock_display=1;
}
void stop_clock_display(){
     clock_display=0;
}
void display_trace_buffers(){
     MsgEnv* message_envelope=request_msg_env();
     get_trace_buffers(message_envelope);
     for(int i=0;i<Max_Env_Data;i++)
             if (message_envelope->msg_data[i]!=NULL)
                printf("%i ",message_envelope->msg_data[i]);
}
void terminate_RTX(){
rtx.terminate();
}
void change_priority(){
}

void CCI(){
     MsgEnv* command;
     while(1){
         command=rtx->receive_message();
         //if(command==NULL){    not needed, receive_message primitive takes care of this =)
         //   cur_proc->status=Blocked_On_Receive;
         //   rtx.blkq_enqueue(Blocked_On_Resource,cur_proc);
         //   process_switch();
         //}
         if (command->msg_data[0]=='s')
            start();
         if (command->msg_data[0]=='p' && command->msg_data[1]=='s')
            list_statuses();
         if (command->msg_data[0]=='c'){
             if (command->msg_data[3]==':'){
                 int h=command->msg_data[1]*10+command->msg_data[2];
                 int m=command->msg_data[4]*10+command->msg_data[5];
                 int s=command->msg_data[7]*10+command->msg_data[8];
                 set_wall_clock(h,m,s);
             }
             else if (command->msg_data[1]=='d')
                  display_clock();
             else if (command->msg_data[1]=='t')
                  stop_clock_display();      
         }
         if (command->msg_data[0]=='b')
            display_trace_buffers();
         if (command->msg_data[0]=='t')
            terminate_RTX();
         if (command->msg_data[0]=='n')
            change_priority();
     }
     rtx->release_msg_env(command);
}


